package h0913;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class GuessPassword {
    public static void main(String[] args) {
        int[] nums={2,3,4};
        int k=2;
        System.out.println(getRes(nums, k));
    }

    private static List<String> getRes(int[] nums, int k) {
        List<String> res=new ArrayList<>();
        Arrays.sort(nums);
        backTrack(res,new LinkedList<>(),nums,k,0);
        res.sort(String::compareTo);
        return res;
    }

    private static void backTrack(List<String> res, LinkedList<String> path, int[] nums, int k,int start) {
        if (path.size()>=k){
            res.add(String.join(",",path));
        }
        for (int i = start; i < nums.length; i++) {
            if (i>0&&nums[i]==nums[i-1]) continue;
            path.addLast(nums[i]+"");
            backTrack(res, path, nums, k, i+1);
            path.removeLast();
        }
    }
}
